package com.nervenets.shopping.controller.user.params;

import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.shopping.enumeration.FlowStatus;
import com.nervenets.shopping.enumeration.OperateType;
import com.nervenets.shopping.enumeration.WealthType;
import com.nervenets.shopping.hibernate.domain.user.WealthFlow;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.constraints.NotNull;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class WealthFlowPagingParams extends EntityPagingParams<WealthFlow> {
    @ApiModelProperty(value = "用户ID，不传时获取当前登录用户的记录，传-1时获取所有用户的记录")
    private long userId;
    @ApiModelProperty(value = "资产类型", required = true)
    @NotNull
    private WealthType wealthType;
    @ApiModelProperty(value = "资产变动方式")
    private OperateType operateType;
    @ApiModelProperty(value = "资产状态")
    private FlowStatus flowStatus;
    @ApiModelProperty(value = "开始时间")
    private long startTime;
    @ApiModelProperty(value = "结束时间")
    private long endTime;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<WealthFlow> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        if (0 == userId) this.userId = JwtUtils.getUser().getId();
        addEqualsPredicate(root.get("userId"), userId, true);
        addEqualsPredicate(root.get("wealthType"), wealthType, null != wealthType);
        addEqualsPredicate(root.get("operateType"), operateType, null != operateType);
        addEqualsPredicate(root.get("flowStatus"), flowStatus, null != flowStatus);
        addBetweenPredicate(root.get("createTime"), startTime, endTime);
    }
}
